package com.zzty.demo.controller;

import com.zzty.demo.entity.Clazz;
import com.zzty.demo.entity.DBMessage;
import com.zzty.demo.entity.User;
import com.zzty.demo.service.UserService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/checkUser",method = RequestMethod.POST)
    public Map checkUser(User user){
        System.out.println("this is checkUser");
        Map<String,String> map=new HashMap<>();
        String result="false1";
        User user1=userService.getUserByName(user);
        logger.info("user1: "+user1.toString());
        if(user1!=null){
            String newPassword = new SimpleHash("md5", user.getPassword(),  ByteSource.Util.bytes(user.getName()), 2).toHex();
            if(!newPassword.equals(user1.getPassword())){
                result="false2";
            }
            else{
                result="success";
            }
        }
        map.put("result",result);
        return map;
    }
    @RequestMapping(value = "getUserByUser")
    public Map getUserByUser(User user){
        User user1=userService.getUserByName(user);
        Object result=null;
        Map<String,Object> results=new HashMap<>();
        if(user1!=null){
            String newPassword =
                    new SimpleHash("md5", user.getPassword(),  ByteSource.Util.bytes(user.getName()), 2).toHex();
            if(newPassword.equals(user1.getPassword())){
                result=user1;
            }
        }
        results.put("result",result);
        return results;
    }

    @RequestMapping(value = "updateImgByUserId/{id}/{img}",method = RequestMethod.POST)
    public boolean updateImgByUserId(@PathParam("id") Integer id,@PathParam("img") String img){
        Integer count=userService.updateImgByUserId(id,img);
        if(count!=0){
            return true;
        }
        else{
            return false;
        }
    }
    @RequestMapping(value = "getDBMessage")
    public DBMessage getDBMessage(){
        DBMessage dbMessage=new DBMessage();
        dbMessage.setUrl("jdbc:mysql://101.132.169.187:3306/ZZ?useUnicode=true&characterEncoding=UTF-8&useSSL=false");
        dbMessage.setUserName("root");
        dbMessage.setPassword("root");
        dbMessage.setDriverName("com.mysql.jdbc.Driver");
        return dbMessage;
    }
    @RequestMapping(value = "/updateOpenIdToUser",method = RequestMethod.POST)
    public Map updateOpenIdToUser(User user){
        logger.info("user: "+user.toString());
        Map<String,String> map=new HashMap<>();
        String result="false";
        Integer count=userService.updateOpenIdToUser(user);
        logger.info("count: "+count);
        if(count==1){
            result="success";
        }
        map.put("result",result);
        return map;
    }

    @RequestMapping("/getUserByOpenId")
    public List getUserByOpenId(User user){
        logger.info("openId: "+user.getOpenId());
        User user1=userService.getUserByOpenId(user);
        logger.info("user1: "+user1.toString());
        List<User> users=new ArrayList<>();
        users.add(user1);
        return users;
    }

    @RequestMapping("/getUserByOpenId1/{openId}")
    public User getUserByOpenId1(@PathVariable("openId") String openId){
        logger.info("openId: "+openId);
        User user =new User();
        user.setOpenId(openId);
        User user1=userService.getUserByOpenId(user);
        logger.info("user1: "+user1.toString());
        return user1;
    }

    @RequestMapping("/getAllUsers")
    public List getAllUsers(){
        List<User> users=userService.getAllUsers();
        return users;
    }

    @RequestMapping("/aaa")
    public Integer aaa(){
        return 1;
    }

    @RequestMapping("/getUserByClazzId")
    public List<User> getUserByClazzId(Clazz clazz){
        logger.info("clazzID: "+clazz.toString());
        List<User> users=new ArrayList<>();
        users=userService.getUserByClazzId(clazz.getId());
        logger.info("users is: "+users.size());
        return users;
    }

    @RequestMapping(value="/checkLogin",method = RequestMethod.POST)
    public Map checkLogin(@RequestBody User user, HttpServletRequest request){
        Map<String,String> result=new HashMap<>();
        String resultValue="false1";
        User user1=userService.getUserByName(user);
        if(user1!=null){
            String newPassword = new SimpleHash("md5", user.getPassword(),  ByteSource.Util.bytes(user.getName()), 2).toHex();
            if(!newPassword.equals(user1.getPassword())){
                resultValue="false2";
            }
            else{
                HttpSession session = request.getSession();
                session.setAttribute("user", user1);
                resultValue=user1.getType();
            }
        }
        result.put("msg",resultValue);
        return result;
    }

    @RequestMapping("/getUser")
    public User getUser(HttpServletRequest request){
        User user =(User)request.getSession().getAttribute("user");
        logger.info("----------user is : "+user.toString());
        return user;
    }

    @RequestMapping(value = "getUserById")
    public User getUserById(User user){
        logger.info("user: "+user.toString());
        User user1=userService.getUserById(user);
        logger.info("user1: "+user1.toString());
        return user1;
    }

    @RequestMapping("/getUserById1/{id}")
    public User getUserById1(@PathVariable("id") Integer id){
        logger.info("id======="+id);
        User user1=userService.getUserById1(id);
        logger.info("user1=========="+user1.toString());
        return user1;
    }

    @RequestMapping("/getUserByOpenId/{openId}")
    public User getUserByOpenId(@PathVariable("openId") String openId){
        logger.info("/user/getUserByOpenId/ openId: "+openId);
        User user1=userService.getUserByOpenId(openId);
        logger.info ("/user/getUserByOpenId/ user1: "+user1.toString());
        if(user1!=null){
            return user1;
        }else{
            return new User();
        }
    }

    @RequestMapping("/getUserByName/{name}")
    public User getUserByName(@PathVariable("name") String name){
        logger.info("UC userName: "+name);
        User user=userService.getUserByName(name);
        logger.info("UC User: "+user.toString());
        return user;
    }

    @RequestMapping("/getUserByClazzId/{id}")
    public List<User> getUserByClazzId(@PathVariable("id") Integer clazzId){
        logger.info("/user/getUserByClazzId/ clazzId: "+clazzId);
        Clazz clazz=new Clazz();
        clazz.setId(clazzId);
        List<User> users=userService.getUserByClazzid(clazz);
        if(users!=null){
            logger.info("/user/getUserByClazzId/UC users: "+users.toString());
            return users;
        }else{
            return null;
        }
    }

    @RequestMapping("/getUserById/{id}")
    public User getUserById(@PathVariable("id") Integer id){
        logger.info("/user/getUserById/ id: "+id);
        User user=userService.getUserById(id);
        logger.info("user======"+user.toString());
        return user;
    }

    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    public Map<String,Object> addUser(User user){
        Map<String,Object> map=new HashMap<>();
        String newPassword = new SimpleHash("md5", user.getPassword(),  ByteSource.Util.bytes(user.getName()), 2).toHex();
        user.setPassword(newPassword);
        Integer count=userService.addUser(user);
        logger.info("count=========="+count);
        if(count ==1){
            map.put("result","success");
        }else{
            map.put("result","false");
        }
        return map;
    }

    @RequestMapping(value = "/getUser2ById",method = RequestMethod.POST)
    public Map getUser2ById(User user){
        logger.info("user===="+user.toString());
        System.out.println("getUser2ById==="+user.toString());
        Map<String,Object> map=new HashMap<>();
        List<User> users=userService.getUser2ById(user);
        if(users!=null||!users.isEmpty()){
            map.put("users",users);
        }else{
            map.put("users",null);
        }
        System.out.println("map==="+map.get("users"));
        return map;
    }
    @RequestMapping(value = "/updateUser")
    public Map<String,Object> updateUser(User user){
        Map<String,Object> map=new HashMap<>();
        Integer count=userService.updateUser(user);
        logger.info("count=========="+count);
        if(count ==1){
            map.put("result","success");
        }else{
            map.put("result","false");
        }
        return map;
    }
}

